//
//  OJConnector.m
//  openJira
//
//  Created by Vincent Demay on 29/04/10.
//  Copyright 2010 Goojet. All rights reserved.
//

#import "OJConnector.h"
#import "OJFilter.h"

#define LOGIN					@"jira1.login"
#define GET_PROPERTIES			@"jira1.getPriorities"
#define GET_ISSUESTYPES			@"jira1.getIssueTypes"
#define GET_FAVOURITEFILTERS	@"jira1.getFavouriteFilters"
#define GET_ISSUESFROMFILTER	@"jira1.getIssuesFromFilter"
#define GET_STATUSES			@"jira1.getStatuses"
#define GET_COMMENTS			@"jira1.getComments"
#define GET_PROJECTS			@"jira1.getProjectsNoSchemes"

#define ADD_COMMENT				@"jira1.addComment"
#define CREATE_ISSUE			@"jira1.createIssue"

static OJConnector* defaultConnector;

@implementation OJConnector

@synthesize priorityList, typeList, statusList, projectList, token;

- (id) init {
	self = [super init];
	if (self != nil) {
		//TODO : param
		NSString *strUrl = [NSString stringWithFormat:@"%@/rpc/xmlrpc", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"jiraBaseUrl"]];
		URL = [NSURL URLWithString:strUrl];
		
		//TODO : cache in a file
		priorityList = nil;
		typeList = nil;
		statusList = nil;
		
		token = nil;
	}
	return self;
}

- (BOOL) loginWithUser:(NSString*) user andPassword:(NSString*) password {
	XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithURL: URL];
	[request setMethod:LOGIN  withParameters:[NSArray arrayWithObjects: user, password, nil]];
	
	NSLog(@"Request body: %@", [request body]);
	
	NSError *error;
	XMLRPCResponse *response = [[[XMLRPCConnection sendSynchronousXMLRPCRequest:request error:&error] object] retain];
	if (error || [response isFault]) {
		[request release];
		return NO;
	} else {
		//memory store token
		token = [response retain];
	}
	
	[request release];
	return YES;
}

- (void) updatePriorities {
	XMLRPCConnectionManager *manager = [XMLRPCConnectionManager sharedManager];
	XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithURL: URL];
	[request setMethod:GET_PROPERTIES withParameters:[NSArray arrayWithObjects: token, nil]];
	[manager spawnConnectionWithXMLRPCRequest:request delegate:self];
}

- (void) updateTypes {
	XMLRPCConnectionManager *manager = [XMLRPCConnectionManager sharedManager];
	XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithURL: URL];
	[request setMethod:GET_ISSUESTYPES withParameters:[NSArray arrayWithObjects: token, nil]];
	[manager spawnConnectionWithXMLRPCRequest:request delegate:self];
}

- (void) updateStatuses {
	XMLRPCConnectionManager *manager = [XMLRPCConnectionManager sharedManager];
	XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithURL: URL];
	[request setMethod:GET_STATUSES withParameters:[NSArray arrayWithObjects: token, nil]];
	[manager spawnConnectionWithXMLRPCRequest:request delegate:self];
}
- (void) updateProjects {
	XMLRPCConnectionManager *manager = [XMLRPCConnectionManager sharedManager];
	XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithURL: URL];
	[request setMethod:GET_PROJECTS withParameters:[NSArray arrayWithObjects: token, nil]];
	[manager spawnConnectionWithXMLRPCRequest:request delegate:self];
}


//////////////////////////////////////////////////////////////////////////////
- (void) getFavouriteFilters:(OJFilterList*) filters {
	XMLRPCConnectionManager *manager = [XMLRPCConnectionManager sharedManager];
	XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithURL: URL];
	[request setMethod:GET_FAVOURITEFILTERS withParameters:[NSArray arrayWithObjects: token, nil]];
	[manager spawnConnectionWithXMLRPCRequest:request delegate:filters];
}

- (void) getIssues:(OJIssueList*) issues fromFilter:(OJFilter *) filter {
	XMLRPCConnectionManager *manager = [XMLRPCConnectionManager sharedManager];
	XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithURL: URL];
	[request setMethod:GET_ISSUESFROMFILTER withParameters:[NSArray arrayWithObjects: token, filter.lid, nil]];
	[manager spawnConnectionWithXMLRPCRequest:request delegate:issues];
}

- (void) getPriorities:(OJPriorityList*) priorities {
	XMLRPCConnectionManager *manager = [XMLRPCConnectionManager sharedManager];
	XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithURL: URL];
	[request setMethod:GET_PROPERTIES withParameters:[NSArray arrayWithObjects: token, nil]];
	[manager spawnConnectionWithXMLRPCRequest:request delegate:priorities];
}

- (void) getTypes:(OJTypeList*) types {
	XMLRPCConnectionManager *manager = [XMLRPCConnectionManager sharedManager];
	XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithURL: URL];
	[request setMethod:GET_ISSUESTYPES withParameters:[NSArray arrayWithObjects: token, nil]];
	[manager spawnConnectionWithXMLRPCRequest:request delegate:types];
}

- (void) getStatuses:(OJStatusList*) statuses {
	XMLRPCConnectionManager *manager = [XMLRPCConnectionManager sharedManager];
	XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithURL: URL];
	[request setMethod:GET_STATUSES withParameters:[NSArray arrayWithObjects: token, nil]];
	[manager spawnConnectionWithXMLRPCRequest:request delegate:statuses];
}

- (void) getComments:(OJCommentList*)comments forIssue:(OJIssue*)issue {
	XMLRPCConnectionManager *manager = [XMLRPCConnectionManager sharedManager];
	XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithURL: URL];
	[request setMethod:GET_COMMENTS withParameters:[NSArray arrayWithObjects: token, issue.key, nil]];
	[manager spawnConnectionWithXMLRPCRequest:request delegate:comments];
}

- (void) getProjects:(OJProjectList*)projects {
	XMLRPCConnectionManager *manager = [XMLRPCConnectionManager sharedManager];
	XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithURL: URL];
	[request setMethod:GET_PROJECTS withParameters:[NSArray arrayWithObjects: token, nil]];
	[manager spawnConnectionWithXMLRPCRequest:request delegate:projects];
}


- (void) addComment:(OJComment *)comment toIssue:(OJIssue*) issue {
	XMLRPCConnectionManager *manager = [XMLRPCConnectionManager sharedManager];
	XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithURL: URL];
	[request setMethod:ADD_COMMENT withParameters:[NSArray arrayWithObjects: token, issue.key, comment.body, nil]];
	[manager spawnConnectionWithXMLRPCRequest:request delegate:comment];
}

- (void) createIssue:(OJIssue*) issue {
	XMLRPCConnectionManager *manager = [XMLRPCConnectionManager sharedManager];
	XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithURL: URL];
	[request setMethod:CREATE_ISSUE withParameters:[NSArray arrayWithObjects: token, [issue toNSDictionnary], nil]];
	[manager spawnConnectionWithXMLRPCRequest:request delegate:issue];
}

//////////////////////////////////////////////////////////////////////////////
+ (OJConnector*) getDefault {
	if (!defaultConnector) {
		defaultConnector = [[OJConnector alloc] init];
	}
	return defaultConnector;
}


////////////////////////// MANAGE RESPONSES ///////////////////
- (void)request: (XMLRPCRequest *)request didReceiveResponse: (XMLRPCResponse *)response {
	if ([request.method isEqual:GET_PROPERTIES]) {
		if (!priorityList) {
			priorityList = [[OJPriorityList alloc] init];
		}
		[priorityList request:request didReceiveResponse:response];
	} else if ([request.method isEqual:GET_ISSUESTYPES]) {
		if (!typeList) {
			typeList = [[OJTypeList alloc] init];
		}
		[typeList request:request didReceiveResponse:response];
	} else if ([request.method isEqual:GET_STATUSES]) {
		if (!statusList) {
			statusList = [[OJStatusList alloc] init];
		}
		[statusList request:request didReceiveResponse:response];
	} else if ([request.method isEqual:GET_PROJECTS]) {
		if (!projectList) {
			projectList = [[OJProjectList alloc] init];
		}
		[projectList request:request didReceiveResponse:response];
	}
}

- (void)request: (XMLRPCRequest *)request didFailWithError: (NSError *)error {
	
}

- (void)request: (XMLRPCRequest *)request didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge {
}

- (void)request: (XMLRPCRequest *)request didCancelAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge {
}

@end
